<template >
    <div v-title data-title="码神之路" style="width:65%;max-width: 80%">

        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="标题" label-width="100">
            <el-input v-model="formInline.title" placeholder="标题"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="formInline.category" placeholder="活动区域" clearable="true">
              <el-option v-for="c in categorys" :key="c.id" :label="c.categoryName" :value="c.id" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="作者">
            <el-input v-model="formInline.author" placeholder="作者"></el-input>
          </el-form-item>
          <el-form-item label="最早发布时间">
            <div class="block">
            
              <el-date-picker
                v-model="formInline.timeStart"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item label="最晚发布时间">
            <div class="block">
              
              <el-date-picker
                v-model="formInline.timeEnd"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>

        <el-table
            :data="articles"
            stripe="true"
            style="width: auto;">
        
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column label="序号" width="50" type="index" >
            </el-table-column>
            <el-table-column label="标题" width="200">
              <template slot-scope="scope">
                <el-popover trigger="hover" placement="top">
                  <p>ID: {{scope.row.id}}</p>
                  <p>标题: {{scope.row.title}}</p>
                  <p>浏览: {{scope.row.viewCounts}}</p>
                  <p>评论: {{scope.row.commentCounts}}</p>
                  <div slot="reference" class="name-wrapper">
                     <a @click="view(scope.row.id)" class="me-article-title">{{scope.row.title}}</a>
                  </div>
                </el-popover>
                
              </template>
            </el-table-column>
            <el-table-column label="作者" width="100">
              <template slot-scope="scope">
                <el-popover trigger="hover" placement="top">
                  <p>ID: {{scope.row.author.id}}</p>
                  <p>账号: {{scope.row.author.account}}</p>
                  <p>昵称: {{scope.row.author.nickname}}</p>
                  <p>邮箱: {{scope.row.author.email}}</p>
                  <p>电话: {{scope.row.author.mobilePhoneNumber}}</p>
                  <div slot="reference" class="name-wrapper">
                     {{scope.row.author.nickname}}
                  </div>
                </el-popover>
                
              </template>
            </el-table-column>
            <el-table-column label="时间" width="200">
              <template slot-scope="scope">
                {{scope.row.createDate}}
              </template>
            </el-table-column>
            <el-table-column label="类型" width="100">
              <template slot-scope="scope">
                {{scope.row.categoryVo.categoryName}}
              </template>
            </el-table-column>
            <el-table-column label="状态" width="100">
              <template slot-scope="scope">
                <el-tag type="success" size="mini" v-if="scope.row.isCloseView == 0">正常</el-tag>
                <el-tag type="warning" size="mini" v-else>封禁</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" >
              <template slot-scope="scope">
                 <el-button
                    size="mini"
                    type="warning"
                    @click="closeArticleView(scope.$index, scope.row)"
                    v-if="scope.row.isCloseView == 0"
                    >
                    封禁</el-button>

                  <el-button
                    size="mini"
                    type="success"
                    @click="openArticleView(scope.$index, scope.row)"
                    v-else
                    >
                    解封
                    </el-button>

                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>

            </el-table-column>
        </el-table>

        <el-pagination
          small="false"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="innerPage.pageNumber"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="innerPage.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="innerPage.totalCounts">
        </el-pagination>
    </div>
</template>

<script>
  import {getArticlesForAdmin} from '@/api/article'
  import {changeArticleViewStatus} from '@/api/article'
  import {deleteArticle} from '@/api/article'
  import {getArticlesByCategory} from '@/api/article'
  import {getAllCategorys} from '@/api/category'
  import {getToken} from '@/request/token'

  export default {
    data() {
      return {
        innerPage: {
          pageSize: 10,
          pageNumber: 1,
          totalCounts: 0,
          name: 'a.createDate',
          sort: 'desc'
        },
        articles: [],
        formInline:{},
        timeStart: '',
        timeEnd: '',
        categorys: []
      }
    },
    props: {
      offset: {
        type: Number,
        default: 100
      },
      page: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    watch: {
      
      'page': {
        handler() {
          this.noData = false
          this.articles = []
          this.innerPage = this.page
          this.getArticles()
        },
        deep: true
      }
    },
    computed: {
      user() {
        let login = this.$store.state.account.length != 0
        let admin = this.$store.state.admin
        return {
          login, admin
        }
      }
    },
    created () {

      let token = getToken()
      if(token == null || this.user.login == false || this.user.admin < 1){
        //跳转到首页
        this.$router.push({path: '/'})
      }else{
        this.getArticles();
        this.getCategorysAndTags();
      }

    },
    methods: {
      load() {
        this.getArticles()
      },
      view(id) {
        this.$router.push({path: `/view/${id}`})
      },
      getArticles() {
        let that = this
        that.loading = true

        let token = getToken()
        getArticlesForAdmin(token, that.innerPage, that.formInline).then(data => {

          let newArticles = data.data.articleList
          that.innerPage.pageSize = data.data.pageData.size
          that.innerPage.pageNumber = data.data.pageData.current
          that.innerPage.totalCounts = data.data.pageData.total
          that.articles = null
          if (newArticles && newArticles.length > 0) {
            that.articles = newArticles
          } else {
            that.noData = true
          }

        }).catch(error => {
          
          if (error !== 'error') {
            that.$message({type: 'error', message: '文章加载失败', showClose: true})
          }
        }).finally(() => {
          that.loading = false
        })

      },
      handleSizeChange(val) {
        let that = this
        that.innerPage.pageSize = val
        this.getArticles()
      },
      handleCurrentChange(val) {
        let that = this
        that.innerPage.pageNumber = val
        this.getArticles()
      },
      openArticleView(index, row){
        let that = this
        let token = getToken()
        changeArticleViewStatus(token, row.id, 0).then(data => {
         
          if(data.success == true){
            
            this.$message({
              message: '解封成功',
              type: 'success'
            });
          }
        }).catch(error => {
            if (error !== 'error') {
              that.$message({type: 'error', message: '文章解封失败!', showClose: true})
            }
        }).finally(() => {
            that.getArticles();
        })
      },
      closeArticleView(index, row){
        let that = this
        let token = getToken()
        changeArticleViewStatus(token, row.id, 1).then(data => {
          if(data.success == true){
            
            this.$message({
              message: '封禁成功',
              type: 'success'
            });
          }
        }).catch(error => {
            if (error !== 'error') {
              that.$message({type: 'error', message: '文章封禁失败!', showClose: true})
            }
        }).finally(() => {
            that.getArticles();
        })
      },
      handleDelete(index, row) {
        let that = this
        let token = getToken()
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteArticle(token, row.id).then(data => {
            if(data.success == true){
              
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
          }).catch(error => {
              if (error !== 'error') {
                that.$message({type: 'error', message: '文章删除失败!', showClose: true})
              }
          }).finally(() => {
              that.getArticles();
          })
        }).catch(() => {
          that.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });

        
      },
      getArticlesByCategory(id) {
        let that = this
        getArticlesByCategory(id).then(data => {
          that.articles = data.data
        }).catch(error => {
          if (error !== 'error') {
            that.$message({type: 'error', message: '文章加载失败', showClose: true})
          }
        })
      },
      onSubmit() {
        this.getArticles();
      },
      getCategorysAndTags() {
        let that = this
        getAllCategorys().then(data => {
          if(data.success){
            that.categorys = data.data
          }else{
             that.$message({type: 'error', message: '文章分类加载失败', showClose: true})
          }

        }).catch(error => {
          if (error !== 'error') {
            that.$message({type: 'error', message: '文章分类加载失败', showClose: true})
          }
        })
      }
      
    }
  }
</script>

<style scoped>

    

</style>